﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ETL.Business.Handlers.ProjectHandlers;
//using ETL.Business.Handlers.ProjectLogHandler;
using ETL.Business.Handlers.ProjectTaskHandler;
using ETL.Business.Handlers.ProjectTimeHandler;
using ETL.Business.Handlers.SessionHandler;
using ETL.Common;
using ETL.Common.Logging;
using ETL.DataAccess;

namespace ETL.Business.Helpers
{
    public class LogOnToTask
    {
        
        //private readonly ProjectLogRepository _projectLogRepository;

        public LogOnToTask()
        {
          //  _projectLogRepository = new ProjectLogRepository();
        }
        //public LogOnToTask(ProjectLogRepository projectLogRepository)
        //{
           // _projectLogRepository = projectLogRepository;
        //}

        public virtual string Execute(short? projectId, short? categoryId,short? taskId, string userId, string userName)
        {
            try
            {
                // Set data to session
                (new SetSessionTask()).Execute(taskId.ToString());

                // Get current project, current task
                var curProject = (new GetProjectById()).Execute(projectId);

                // Save log
             //   (new AddNewProjectLog()).Execute(projectId,categoryId, taskId, userId, "about to log onto new task");

                // Check whether this project is completed of suspended)
                //if (curProject.ProjectCompleted == true)
                if (curProject.ProjectStatus == "COMPLETED")
                {
                    return Constants.TaskLogonState.Completed;
                }
                //else if (curProject.ProjectSuspended == true)
                if (curProject.ProjectStatus == "SUSPENDED")
                {
                    return Constants.TaskLogonState.Suspended;
                }

                // set resuming
                (new SetSessionResuming()).Execute("False");

                // log on to task
                (new LogOn()).Execute(projectId, categoryId, taskId, userName, userId);

                // return success state)
                return Constants.TaskLogonState.Successful;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return Constants.TaskLogonState.Failed;
            }

        }
    }
}
